{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Factoring Polynomials with SymPy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is an example that uses [SymPy](http://sympy.org/en/index.html) to factor polynomials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from ipywidgets import interact"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sympy import Symbol, Eq, factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = Symbol('x')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def factorit(n):\n",
    "    return Eq(x**n-1, factor(x**n-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "x**12 - 1 == (x - 1)*(x + 1)*(x**2 + 1)*(x**2 - x + 1)*(x**2 + x + 1)*(x**4 - x**2 + 1)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factorit(12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA9oAAAAvCAYAAAAcoLJwAAAH3ElEQVR4Xu3dy2sdZRgH4MlV0m6MroSC1AvYpI2GNi4EQXduXHQj7dYL4p8g6sY/wpWX4AVFEV0IboTWG4ppiKY0Loqm0IBuxNBL2qbJOb5fm0PbGDMn9lxm5jwDLznp+Trzfs83gfxy5szpy2wECBAgQIAAAQIECBAgQIBAywT6WrYnOyJAgAABAgQIECBAgAABAgQyQdtJQIAA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c5wkQIECAAAECBAgQIFAUAUG7KCuhDwIECBAgQIAAAQIECBCohICgXYllNAkCBAgQIECAAAECBAgQKIqAoF2UldAHAQIECBAgQIAAAQIECFRCQNCuxDKaBAECBAgQIECAAAECBAgURUDQLspK6IMAAQIECBAgQIAAAQIEKiEgaFdiGU2CAAECBAgQIECAAAECBIoiIGgXZSX0QYAAAQIECBAgQIAAAQKVEPgHRASBPzHwugYAAAAASUVORK5CYII=",
      "text/html": [
       "<img src= class=\"jupyter-widget\">\n",
       "<script type=\"application/vnd.jupyter-embedded-widgets\">[{},{\"layout\":\"IPY_MODEL_77f2062df66e4781a624d87f0e54549d\",\"min\":2,\"description\":\"n\",\"max\":40,\"value\":21},{},{\"children\":[\"IPY_MODEL_8139a32e46e047808fae364be1309f4c\"],\"layout\":\"IPY_MODEL_b37a5b98616644f4970c9fa8e180e4e5\",\"_dom_classes\":[\"widget-interact\"]}]</script>"
      ]
     },
     "metadata": {
      "isWidgetSnapshot": true
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "x**21 - 1 == (x - 1)*(x**2 + x + 1)*(x**6 + x**5 + x**4 + x**3 + x**2 + x + 1)*(x**12 - x**11 + x**9 - x**8 + x**6 - x**4 + x**3 - x + 1)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "interact(factorit, n=(2,40));"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3+"
  },
  "widgets": {
   "state": {
    "77f2062df66e4781a624d87f0e54549d": {
     "views": []
    },
    "8139a32e46e047808fae364be1309f4c": {
     "views": []
    },
    "a9e505fd895a4e0685243e7428a7c6be": {
     "views": [
      {
       "cell": {
        "cell_type": "code",
        "execution_count": 6,
        "metadata": {
         "collapsed": false,
         "trusted": true
        },
        "outputs": [
         {
          "data": {
           "image/png": "iVBORw0KGgoAAAANSUhEUgAAA9oAAAAvCAYAAAAcoLJwAAAH3ElEQVR4Xu3dy2sdZRgH4MlV0m6MroSC1AvYpI2GNi4EQXduXHQj7dYL4p8g6sY/wpWX4AVFEV0IboTWG4ppiKY0Loqm0IBuxNBL2qbJOb5fm0PbGDMn9lxm5jwDLznp+Trzfs83gfxy5szpy2wECBAgQIAAAQIECBAgQIBAywT6WrYnOyJAgAABAgQIECBAgAABAgQyQdtJQIAAAQIECBAgQIAAAQIEWiggaLcQ064IECBAgAABAgQIECBAgICg7RwgQIAAAQIECBAgQIAAAQItFBC0W4hpVwQIECBAgAABAgQIECBAQNB2DhAgQIAAAQIECBAgQIAAgRYKCNotxLQrAgQIECBAgAABAgQIECAgaDsHCBAgQIAAAQIECBAgQIBACwUE7RZi2hUBAgQIECBAgAABAgQIEBC0nQMECBAgQIAAAQIECBAgQKCFAoJ2CzHtqnmBkydPPtHf338s73/09fVlIyMjo3v37l3OG+t5AgQIECBAgAABAgQIFEFA0C7CKvRYD4uLi3devnz572amXavVrg0bHx93rjYDZgwBAgQIECBAgAABAl0XEF66vgS910B6NXtoaOhYerU6b0tBO5WgnSfleQIECBAgQIAAAQIEiiKQn3SK0qk+KiMgaFdmKU2EAAECBAgQIECAAIEtBARtp0XHBQTtjpM7IAECBAgQIECAAAECHRQQtDuI7VDXBQRtZwIBAgQIECBAgAABAlUWELSrvLoFnZugXdCF0RYBAgQIECBAgAABAi0RELRbwmgnOxEQtHeiVcixD0ZXb0VNRs1EvRD1202dPhSPX4/6IurdQs5AUwQIECBAgAABAgTaKCBotxHXrrcWELRLf2a8HzN4L2ol6o2otY3QnSY2EPV41AdRr0RNl362JkCAAAECBAgQIEBghwKC9g7BDL99AUH79g27uIfROPZ9UbMbPTwWX7+Puivq5s9G/ya+f1vQ7uJKOTQBAgQIECBAgEDXBATtG/QPx8PXohaih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fv373eu5moZQIAAAQIECBAgQIBAEQSElyKsQo/10AjajRC93fTTmFqtJmj32DliugQIECBAgAABAgTKLCBol3n1Stp7I2inG53lbSlkpxofH88fnLczzxMgQIAAAQIECBAgQKADAsJLB5Ad4laBRtAeGBjIpUkhe21tTdDOlTKAAAECBAgQIECAAIGiCAjaRVmJHurj1KlTj8SdxOeGh4dzZ51Cdnov99jYmHM1V8sAAgQIECBAgAABAgSKICC8FGEVerCH06dP11dXV7NmLh+PsH04Pkf78x5kMmUCBAgQIECAAAECBEooIGiXcNGq0nK6hDxvLoODg2f27dt3Jm+c5wkQIECAAAECBAgQIFAUAUG7KCuhDwIECBAgQIAAAQIECBCohICgXYllNAkCBAgQIECAAAECBAgQKIqAoF2UldAHAQIECBAgQIAAAQIECFRCQNCuxDKaBAECBAgQIECAAAECBAgURUDQLspK6IMAAQIECBAgQIAAAQIEKiEgaFdiGU2CAAECBAgQIECAAAECBIoiIGgXZSX0QYAAAQIECBAgQIAAAQKVEPgHRASBPzHwugYAAAAASUVORK5CYII=",
           "text/html": "<img src= class=\"jupyter-widget\">\n<script type=\"application/vnd.jupyter-embedded-widgets\">[{},{\"layout\":\"IPY_MODEL_77f2062df66e4781a624d87f0e54549d\",\"min\":2,\"description\":\"n\",\"max\":40,\"value\":21},{},{\"children\":[\"IPY_MODEL_8139a32e46e047808fae364be1309f4c\"],\"layout\":\"IPY_MODEL_b37a5b98616644f4970c9fa8e180e4e5\",\"_dom_classes\":[\"widget-interact\"]}]</script>"
          },
          "metadata": {
           "isWidgetSnapshot": true
          },
          "output_type": "display_data"
         },
         {
          "data": {
           "text/plain": "x**21 - 1 == (x - 1)*(x**2 + x + 1)*(x**6 + x**5 + x**4 + x**3 + x**2 + x + 1)*(x**12 - x**11 + x**9 - x**8 + x**6 - x**4 + x**3 - x + 1)"
          },
          "metadata": {},
          "output_type": "display_data"
         }
        ],
        "source": "interact(factorit, n=(2,40));"
       },
       "cell_index": 7,
       "root": true
      }
     ]
    },
    "b37a5b98616644f4970c9fa8e180e4e5": {
     "views": []
    }
   },
   "version": "0.0.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
